#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>

const int N = 1e5 + 5;
int n, m, s;
vector<int> dis(N,0x3f3f3f3f);
vector<pii> adj[N];

int main(){
    cin>>n>>m>>s;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        adj[u].push_back({v,w});
    }
    dis[s]=0;
    priority_queue<pii,vector<pii>,greater<pii>>q;
    q.push({0,s});
    while(!q.empty()){
        auto t=q.top();
        q.pop();
        int id=t.second;
        if(t.first!=dis[id]){
            continue;
        }
        for(auto x:adj[id]){
            int it = x.first,d=t.first+x.second;
            if(d<dis[it])q.push({dis[it]=d,it});
        }
    }
    for(int i=1;i<=n;i++){
        cout<<dis[i]<<' ';
    }
}